home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 1999 August / SGI Freeware 1999 August.iso / dist / fw_xemacs.idb / usr / freeware / lib / xemacs-20.4 / lisp / w3 / url-cookie.el.z / url-cookie.el
Encoding:
Text File  |  1998-05-21  |  13.3 KB  |  389 lines

  1. ;;; url-cookie.el --- Netscape Cookie support
  2. ;; Author: wmperry
  3. ;; Created: 1997/10/28 16:32:00
  4. ;; Version: 1.20
  5. ;; Keywords: comm, data, processes, hypermedia
  6.  
  7. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  8. ;;; Copyright (c) 1996 by William M. Perry <wmperry@cs.indiana.edu>
  9. ;;; Copyright (c) 1996, 1997 Free Software Foundation, Inc.
  10. ;;;
  11. ;;; This file is not part of GNU Emacs, but the same permissions apply.
  12. ;;;
  13. ;;; GNU Emacs is free software; you can redistribute it and/or modify
  14. ;;; it under the terms of the GNU General Public License as published by
  15. ;;; the Free Software Foundation; either version 2, or (at your option)
  16. ;;; any later version.
  17. ;;;
  18. ;;; GNU Emacs is distributed in the hope that it will be useful,
  19. ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  21. ;;; GNU General Public License for more details.
  22. ;;;
  23. ;;; You should have received a copy of the GNU General Public License
  24. ;;; along with GNU Emacs; see the file COPYING.  If not, write to the
  25. ;;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  26. ;;; Boston, MA 02111-1307, USA.
  27. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  28.  
  29. (require 'timezone)
  30. (require 'cl)
  31.  
  32. (eval-and-compile
  33.   (let ((keywords 
  34.      '(:name :value :expires :path :domain :test :secure)))
  35.     (while keywords
  36.       (or (boundp (car keywords))
  37.       (set (car keywords) (car keywords)))
  38.       (setq keywords (cdr keywords)))))
  39.  
  40. ;; See http://home.netscape.com/newsref/std/cookie_spec.html for the
  41. ;; 'open standard' defining this crap.
  42. ;;
  43. ;; A cookie is stored internally as a vector of 7 slots
  44. ;; [ 'cookie name value expires path domain secure ]
  45.  
  46. (defsubst url-cookie-name    (cookie) (aref cookie 1))
  47. (defsubst url-cookie-value   (cookie) (aref cookie 2))
  48. (defsubst url-cookie-expires (cookie) (aref cookie 3))
  49. (defsubst url-cookie-path    (cookie) (aref cookie 4))
  50. (defsubst url-cookie-domain  (cookie) (aref cookie 5))
  51. (defsubst url-cookie-secure  (cookie) (aref cookie 6))
  52.  
  53. (defsubst url-cookie-set-name    (cookie val) (aset cookie 1 val))
  54. (defsubst url-cookie-set-value   (cookie val) (aset cookie 2 val))
  55. (defsubst url-cookie-set-expires (cookie val) (aset cookie 3 val))
  56. (defsubst url-cookie-set-path    (cookie val) (aset cookie 4 val))
  57. (defsubst url-cookie-set-domain  (cookie val) (aset cookie 5 val))
  58. (defsubst url-cookie-set-secure  (cookie val) (aset cookie 6 val))
  59. (defsubst url-cookie-retrieve-arg (key args) (nth 1 (memq key args)))
  60.  
  61. (defsubst url-cookie-create (&rest args)
  62.   (let ((retval (make-vector 7 nil)))
  63.     (aset retval 0 'cookie)
  64.     (url-cookie-set-name retval (url-cookie-retrieve-arg :name args))
  65.     (url-cookie-set-value retval (url-cookie-retrieve-arg :value args))
  66.     (url-cookie-set-expires retval (url-cookie-retrieve-arg :expires args))
  67.     (url-cookie-set-path retval (url-cookie-retrieve-arg :path args))
  68.     (url-cookie-set-domain retval (url-cookie-retrieve-arg :domain args))
  69.     (url-cookie-set-secure retval (url-cookie-retrieve-arg :secure args))
  70.     retval))
  71.  
  72. (defun url-cookie-p (obj)
  73.   (and (vectorp obj) (= (length obj) 7) (eq (aref obj 0) 'cookie)))
  74.  
  75. ;;;###autoload
  76. (defun url-cookie-parse-file (&optional fname)
  77.   (setq fname (or fname url-cookie-file))
  78.   (condition-case ()
  79.       (load fname nil t)
  80.     (error (message "Could not load cookie file %s" fname))))
  81.  
  82. (defun url-cookie-clean-up (&optional secure)
  83.   (let* (
  84.      (var (if secure 'url-cookie-secure-storage 'url-cookie-storage))
  85.      (val (symbol-value var))
  86.      (cur nil)
  87.      (new nil)
  88.      (cookies nil)
  89.      (cur-cookie nil)
  90.      (new-cookies nil)
  91.      )
  92.     (while val
  93.       (setq cur (car val)
  94.         val (cdr val)
  95.         new-cookies nil
  96.         cookies (cdr cur))
  97.       (while cookies
  98.     (setq cur-cookie (car cookies)
  99.           cookies (cdr cookies))
  100.     (if (or (not (url-cookie-p cur-cookie))
  101.         (url-cookie-expired-p cur-cookie)
  102.         (null (url-cookie-expires cur-cookie)))
  103.         nil
  104.       (setq new-cookies (cons cur-cookie new-cookies))))
  105.       (if (not new-cookies)
  106.       nil
  107.     (setcdr cur new-cookies)
  108.     (setq new (cons cur new))))
  109.     (set var new)))
  110.  
  111. ;;;###autoload
  112. (defun url-cookie-write-file (&optional fname)
  113.   (setq fname (or fname url-cookie-file))
  114.   (url-cookie-clean-up)
  115.   (url-cookie-clean-up t)
  116.   (save-excursion
  117.     (set-buffer (get-buffer-create " *cookies*"))
  118.     (erase-buffer)
  119.     (fundamental-mode)
  120.     (insert ";; Emacs-W3 HTTP cookies file\n"
  121.         ";; Automatically generated file!!! DO NOT EDIT!!!\n\n"
  122.         "(setq url-cookie-storage\n '")
  123.     (pp url-cookie-storage (current-buffer))
  124.     (insert ")\n(setq url-cookie-secure-storage\n '")
  125.     (pp url-cookie-secure-storage (current-buffer))
  126.     (insert ")\n")
  127.     (write-file fname)
  128.     (kill-buffer (current-buffer))))
  129.  
  130. (defun url-cookie-store (name value &optional expires domain path secure)
  131.   "Stores a netscape-style cookie"
  132.   (let* ((storage (if secure url-cookie-secure-storage url-cookie-storage))
  133.      (tmp storage)
  134.      (cur nil)
  135.      (found-domain nil))
  136.  
  137.     ;; First, look for a matching domain
  138.     (setq found-domain (assoc domain storage))
  139.  
  140.     (if found-domain
  141.     ;; Need to either stick the new cookie in existing domain storage
  142.     ;; or possibly replace an existing cookie if the names match.
  143.     (progn
  144.       (setq storage (cdr found-domain)
  145.         tmp nil)
  146.       (while storage
  147.         (setq cur (car storage)
  148.           storage (cdr storage))
  149.         (if (and (equal path (url-cookie-path cur))
  150.              (equal name (url-cookie-name cur)))
  151.         (progn
  152.           (url-cookie-set-expires cur expires)
  153.           (url-cookie-set-value cur value)
  154.           (setq tmp t))))
  155.       (if (not tmp)
  156.           ;; New cookie
  157.           (setcdr found-domain (cons
  158.                     (url-cookie-create :name name
  159.                                :value value
  160.                                :expires expires
  161.                                :domain domain
  162.                                :path path
  163.                                :secure secure)
  164.                     (cdr found-domain)))))
  165.       ;; Need to add a new top-level domain
  166.       (setq tmp (url-cookie-create :name name
  167.                    :value value
  168.                    :expires expires
  169.                    :domain domain
  170.                    :path path
  171.                    :secure secure))
  172.       (cond
  173.        (storage
  174.     (setcdr storage (cons (list domain tmp) (cdr storage))))
  175.        (secure
  176.     (setq url-cookie-secure-storage (list (list domain tmp))))
  177.        (t
  178.     (setq url-cookie-storage (list (list domain tmp))))))))
  179.  
  180. (defun url-cookie-expired-p (cookie)
  181.   (let* (
  182.      (exp (url-cookie-expires cookie))
  183.      (cur-date (and exp (timezone-parse-date (current-time-string))))
  184.      (exp-date (and exp (timezone-parse-date exp)))
  185.      (cur-greg (and cur-date (timezone-absolute-from-gregorian
  186.                   (string-to-int (aref cur-date 1))
  187.                   (string-to-int (aref cur-date 2))
  188.                   (string-to-int (aref cur-date 0)))))
  189.      (exp-greg (and exp (timezone-absolute-from-gregorian
  190.                  (string-to-int (aref exp-date 1))
  191.                  (string-to-int (aref exp-date 2))
  192.                  (string-to-int (aref exp-date 0)))))
  193.      (diff-in-days (and exp (- cur-greg exp-greg)))
  194.      )
  195.     (cond
  196.      ((not exp)    nil)            ; No expiry == expires at browser quit
  197.      ((< diff-in-days 0) nil)        ; Expires sometime after today
  198.      ((> diff-in-days 0) t)        ; Expired before today
  199.      (t                    ; Expires sometime today, check times
  200.       (let* ((cur-time (timezone-parse-time (aref cur-date 3)))
  201.          (exp-time (timezone-parse-time (aref exp-date 3)))
  202.          (cur-norm (+ (* 360 (string-to-int (aref cur-time 2)))
  203.               (*  60 (string-to-int (aref cur-time 1)))
  204.               (*   1 (string-to-int (aref cur-time 0)))))
  205.          (exp-norm (+ (* 360 (string-to-int (aref exp-time 2)))
  206.               (*  60 (string-to-int (aref exp-time 1)))
  207.               (*   1 (string-to-int (aref exp-time 0))))))
  208.     (> (- cur-norm exp-norm) 1))))))
  209.  
  210. ;;;###autoload
  211. (defun url-cookie-retrieve (host path &optional secure)
  212.   "Retrieves all the netscape-style cookies for a specified HOST and PATH"
  213.   (let ((storage (if secure
  214.              (append url-cookie-secure-storage url-cookie-storage)
  215.            url-cookie-storage))
  216.     (case-fold-search t)
  217.     (cookies nil)
  218.     (cur nil)
  219.     (retval nil)
  220.     (path-regexp nil))
  221.     (while storage
  222.       (setq cur (car storage)
  223.         storage (cdr storage)
  224.         cookies (cdr cur))
  225.       (if (and (car cur)
  226.            (string-match (concat "^.*" (regexp-quote (car cur)) "$") host))
  227.       ;; The domains match - a possible hit!
  228.       (while cookies
  229.         (setq cur (car cookies)
  230.           cookies (cdr cookies)
  231.           path-regexp (concat "^" (regexp-quote
  232.                        (url-cookie-path cur))))
  233.         (if (and (string-match path-regexp path)
  234.              (not (url-cookie-expired-p cur)))
  235.         (setq retval (cons cur retval))))))
  236.     retval))
  237.  
  238. ;;;###autolaod
  239. (defun url-cookie-generate-header-lines (host path secure)
  240.   (let* ((cookies (url-cookie-retrieve host path secure))
  241.     (retval nil)
  242.     (cur nil)
  243.     (chunk nil))
  244.     ;; Have to sort this for sending most specific cookies first
  245.     (setq cookies (and cookies
  246.                (sort cookies
  247.                  (function
  248.                   (lambda (x y)
  249.                 (> (length (url-cookie-path x))
  250.                    (length (url-cookie-path y))))))))
  251.     (while cookies
  252.       (setq cur (car cookies)
  253.         cookies (cdr cookies)
  254.         chunk (format "%s=%s" (url-cookie-name cur) (url-cookie-value cur))
  255.         retval (if (< 80 (+ (length retval) (length chunk) 4))
  256.                (concat retval "\r\nCookie: " chunk)
  257.              (if retval
  258.              (concat retval "; " chunk)
  259.                (concat "Cookie: " chunk)))))
  260.     (if retval
  261.     (concat retval "\r\n")
  262.       "")))
  263.  
  264. (defvar url-cookie-two-dot-domains
  265.   (concat "\\.\\("
  266.    (mapconcat 'identity (list "com" "edu" "net" "org" "gov" "mil" "int")
  267.           "\\|")
  268.    "\\)$")
  269.   "A regular expression of top-level domains that only require two matching
  270. '.'s in the domain name in order to set a cookie.")
  271.  
  272. (defcustom url-cookie-trusted-urls nil
  273.   "*A list of regular expressions matching URLs to always accept cookies from."
  274.   :type '(repeat regexp)
  275.   :group 'url-cookie)
  276.  
  277. (defcustom url-cookie-untrusted-urls nil
  278.   "*A list of regular expressions matching URLs to never accept cookies from."
  279.   :type '(repeat regexp)
  280.   :group 'url-cookie)
  281.  
  282. (defun url-cookie-host-can-set-p (host domain)
  283.   (let ((numdots 0)
  284.     (tmp domain)
  285.     (last nil)
  286.     (case-fold-search t)
  287.     (mindots 3))
  288.     (while (setq last (string-match "\\." host last))
  289.       (setq numdots (1+ numdots)
  290.         last (1+ last)))
  291.     (if (string-match url-cookie-two-dot-domains domain)
  292.     (setq mindots 2))
  293.     (cond
  294.      ((string= host domain)        ; Apparently netscape lets you do this
  295.       t)
  296.      ((< numdots mindots)        ; Not enough dots in domain name!
  297.       nil)
  298.      (t
  299.       (string-match (concat (regexp-quote domain) "$") host)))))
  300.  
  301. (defun url-header-comparison (x y)
  302.   (string= (downcase x) (downcase y)))
  303.  
  304. ;;;###autoload
  305. (defun url-cookie-handle-set-cookie (str)
  306.   (let* ((args (mm-parse-args str nil t)) ; Don't downcase names
  307.      (case-fold-search t)
  308.      (secure (and (assoc* "secure" args :test 'url-header-comparison) t))
  309.      (domain (or (cdr-safe (assoc* "domain" args :test
  310.                        'url-header-comparison))
  311.              (url-host url-current-object)))
  312.      (current-url (url-view-url t))
  313.      (trusted url-cookie-trusted-urls)
  314.      (untrusted url-cookie-untrusted-urls)
  315.      (expires (cdr-safe (assoc* "expires" args :test
  316.                     'url-header-comparison)))
  317.      (path (or (cdr-safe (assoc* "path" args :test
  318.                      'url-header-comparison))
  319.            (file-name-directory
  320.             (url-filename url-current-object))))
  321.      (rest nil))
  322.     (while args
  323.       (if (not (member (downcase (car (car args)))
  324.                '("secure" "domain" "expires" "path")))
  325.       (setq rest (cons (car args) rest)))
  326.       (setq args (cdr args)))
  327.  
  328.     ;; Sometimes we get dates that the timezone package cannot handle very
  329.     ;; gracefully - take care of this here, instead of in url-cookie-expired-p
  330.     ;; to speed things up.
  331.     (if (and expires
  332.          (string-match
  333.           (concat "^[^,]+, +\\(..\\)-\\(...\\)-\\(..\\) +"
  334.               "\\(..:..:..\\) +\\[*\\([^\]]+\\)\\]*$")
  335.           expires))
  336.     (setq expires (concat (url-match expires 1) " "
  337.                   (url-match expires 2) " "
  338.                   (url-match expires 3) " "
  339.                   (url-match expires 4) " ["
  340.                   (url-match expires 5) "]")))
  341.     (while (consp trusted)
  342.       (if (string-match (car trusted) current-url)
  343.       (setq trusted (- (match-end 0) (match-beginning 0)))
  344.     (pop trusted)))
  345.     (while (consp untrusted)
  346.       (if (string-match (car untrusted) current-url)
  347.       (setq untrusted (- (match-end 0) (match-beginning 0)))
  348.     (pop untrusted)))
  349.     (if (and trusted untrusted)
  350.     ;; Choose the more specific match
  351.     (if (> trusted untrusted)
  352.         (setq untrusted nil)
  353.       (setq trusted nil)))
  354.     (cond
  355.      (untrusted
  356.       ;; The site was explicity marked as untrusted by the user
  357.       nil)
  358.      ((and (listp url-privacy-level) (memq 'cookies url-privacy-level))
  359.       ;; user never wants cookies
  360.       nil)
  361.      ((and url-cookie-confirmation
  362.        (not trusted)
  363.        (save-window-excursion
  364.          (with-output-to-temp-buffer "*Cookie Warning*"
  365.            (mapcar
  366.         (function
  367.          (lambda (x)
  368.            (princ (format "%s - %s" (car x) (cdr x))))) rest))
  369.          (prog1
  370.          (not (funcall url-confirmation-func
  371.                    (format "Allow %s to set these cookies? "
  372.                        (url-host url-current-object))))
  373.            (if (get-buffer "*Cookie Warning*")
  374.            (kill-buffer "*Cookie Warning*")))))
  375.       ;; user wants to be asked, and declined.
  376.       nil)
  377.      ((url-cookie-host-can-set-p (url-host url-current-object) domain)
  378.       ;; Cookie is accepted by the user, and passes our security checks
  379.       (let ((cur nil))
  380.     (while rest
  381.       (setq cur (pop rest))
  382.       (url-cookie-store (car cur) (cdr cur)
  383.                 expires domain path secure))))
  384.      (t
  385.       (message "%s tried to set a cookie for domain %s - rejected."
  386.            (url-host url-current-object) domain)))))
  387.  
  388. (provide 'url-cookie)
  389.